# -*- coding: utf-8 -*-
import pandas as pd
import os
import datetime
import zipfile


class MyIO:
    def __init__(self):
        pass

    def FilesToZip(self, foldeUrl, fileName):
        zipFileName = os.path.join(foldeUrl, fileName).replace('.xlsx', '.zip')
        f = zipfile.ZipFile(zipFileName, 'w', zipfile.ZIP_DEFLATED)

        file_url = os.path.join(foldeUrl, fileName)
        f.write(file_url)
        f.close()

    def XlsxsToZip(self,zipfile_name,file_list):
        f = zipfile.ZipFile(zipfile_name,'w',zipfile.ZIP_DEFLATED)
        for i in file_list:
            f.write(i)
        f.close()

    # 获取常用路径
    def GetPath(self,current_hub,current_model):
        work_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
        input_path = os.path.join(os.path.join(work_path,'input',current_model,current_hub))
        output_path = os.path.join(os.path.join(work_path,'output',current_model,current_hub))
        common_path = os.path.join(os.path.join(work_path,'input',current_model,'common'))
        temp_path = os.path.join(os.path.join(work_path,'temp',current_model,current_hub))
        return input_path, output_path, common_path,temp_path

    def GetNewInputPath(self):
         work_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
         input_path = os.path.join(os.path.join(work_path,'input'))
         return input_path

    def GetInputPath(self,current_hub,current_model):
        work_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
        input_path = os.path.join(os.path.join(work_path,'input',current_model,current_hub))
        return input_path

    def GetOutputPath(self,current_hub):
        work_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
        output_path = os.path.join(os.path.join(work_path,'output',current_hub))
        if not os.path.exists(output_path):
            os.makedirs(output_path)
        return output_path
    def GetTempPath(self,current_hub,current_model):
        work_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
        temp_path = os.path.join(os.path.join(work_path,'temp',current_model,current_hub))
        return temp_path

    def GetCommonPath(self,current_model):
        work_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
        common_path = os.path.join(os.path.join(work_path,'input',current_model,'common'))
        return common_path

    def GetLogPath(self):
        work_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
        log_path =  os.path.join(work_path,'log')
        if not os.path.exists(log_path):
            os.makedirs(log_path)
        return log_path

    # 读取csv或excel文件, 入参文件名和文件路径
    def ReadCsvOrExcel(self,file_name, file_path):
        file_name_short = os.path.splitext(file_name)[0]
        csv_file_name = os.path.join(file_path,file_name_short+'.csv')
        excel_file_name =  os.path.join(file_path,file_name_short+'.xlsx')
        if os.path.exists(csv_file_name):
            return pd.read_csv(csv_file_name, encoding='gb2312',engine='python')
        elif os.path.exists(excel_file_name):
            return pd.read_excel(excel_file_name)
        else:
            raise Exception("fail to find file:" + file_name_short)

    # 读取csv或excel文件, 入参文件名和文件路径
    def ReadExcelSheet(self,excel_file_name, sheet_name):
        if os.path.exists(excel_file_name):
            return pd.read_excel(excel_file_name,sheetname =sheet_name)
        else:
            raise Exception("fail to find file:" + excel_file_name+"."+sheet_name)

    # 输出文件
    def OutputData(self,output_path,filename, data):
        if not os.path.exists(output_path):
            os.makedirs(output_path)
        time_str = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        filename2 = filename + "_" + time_str + ".xlsx"
        result_file = os.path.join(output_path,filename2)
        writer = pd.ExcelWriter(result_file)
        data.to_excel(writer,filename,index=False)
        writer.save()
        writer.close()

    # 输出文件
    def OutputData2(self,output_path, data):
        writer = pd.ExcelWriter(output_path)
        data.to_excel(writer,index=False)
        writer.save()
        writer.close()

    # 输出Excel,有多个sheet
    def OutputExcelFile(self,url, sheets):
        writer = pd.ExcelWriter(url)
        for sheet,data in sheets.items():
            data.to_excel(writer,sheet,index=False)
        writer.save()
        writer.close()
